<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加员工</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
</head>
<body>
    <div class="layui-container">
        <form class="layui-form" action="/app/ky_shouyin/admin/staff/add" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">登录账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" lay-verify="required" placeholder="请输入登录账号，必须英文字母" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">登录密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" lay-verify="required" placeholder="请输入登录密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="confirm_password" lay-verify="required|confirmPass" placeholder="请再次输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">员工姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入员工姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">手机号码</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" lay-verify="required|phone" placeholder="请输入手机号码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属门店</label>
                <div class="layui-input-inline">
                    <select name="store_id" lay-verify="required">
                        <option value="">请选择门店</option>
                        <?php foreach ($stores as $store): ?>
                            <option value="<?= $store->id ?>"><?= $store->name ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">员工角色</label>
                <div class="layui-input-inline">
                    <select name="role" lay-verify="required">
                        <option value="">请选择角色</option>
                        <option value="2">管理员</option>
                        <option value="3">店长</option>
                        <option value="4">收银员</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">员工状态</label>
                <div class="layui-input-inline">
                    <select name="status" lay-verify="required">
                        <option value="0">已启用</option>
                        <option value="1">已禁用</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit lay-filter="add-staff">立即添加</button>
            </div>
        </form>
    </div>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['form', 'layer'], function(){
        var $ = layui.$
        ,form = layui.form
        ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            confirmPass: function(value){
                if($('input[name=password]').val() !== value)
                    return '两次密码输入不一致！';
            }
        });
        
        // 监听提交
        form.on('submit(add-staff)', function(data){
            $.ajax({
                url: '/app/ky_shouyin/admin/staff/add'
                ,type: 'POST'
                ,data: data.field
                ,success: function(res){
                    if(res.code === 0){
                        layer.msg('添加成功', {
                            icon: 1
                            ,time: 1500
                        }, function(){
                            // 关闭当前弹窗
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            // 刷新父页面表格
                            parent.layui.table.reload('LAY-staff-list');
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
            });
            return false;
        });
        
        // 添加关闭按钮事件
        window.closeLayer = function(){
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    });
    </script>
</body>
</html> 